home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
1833
/
1833.xpi
/
chrome
/
yoono.jar
/
ff2
/
bookmarks-restore.js
next >
Wrap
Text File
|
2009-12-16
|
7KB
|
237 lines
/*!
* bkmRestoreService
*
* Restore/Save bookmarks changes
* @author : david marteau <marteau.david@free.fr>
*/
const CI = Components.interfaces;
const CL = Components.classes;
const RDF = CL['@mozilla.org/rdf/rdf-service;1'].getService(CI.nsIRDFService);
const RDFC = CL["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils);
const DIRS = CL['@mozilla.org/file/directory_service;1'].getService(CI.nsIProperties);
const NC_NS = "http://home.netscape.com/NC-rdf#";
const NC_NS_CMD = NC_NS + "command?cmd=";
const WMED = CL['@mozilla.org/appshell/window-mediator;1'].getService(CI.nsIWindowMediator);
var BMKS = null;
const NAME_RESOURCE = RDF.GetResource("http://home.netscape.com/NC-rdf#Name");
function RDFGetName(resource) {
var nameRes = gDataSource.GetTarget(resource, NAME_RESOURCE, true);
if (nameRes)
return nameRes.QueryInterface(CI.nsIRDFLiteral).Value;
return "";
}
function getDefaultPersonalToolbarName() {
var win=WMED.getMostRecentWindow("navigator:browser");
return win.document.getElementById("PersonalToolbar").getAttribute("toolbarname");
}
/*!
* Execute a command with the given source and arguments
*/
function doBookmarksCommand(aSource, aCommand, aArgumentsArray)
{
var rCommand = RDF.GetResource(aCommand);
const kSuppArrayContractID = "@mozilla.org/supports-array;1";
const kSuppArrayIID = CI.nsISupportsArray;
var sourcesArray = CL[kSuppArrayContractID].createInstance(kSuppArrayIID);
if(aSource) {
sourcesArray.AppendElement(aSource);
}
var argsArray = CL[kSuppArrayContractID].createInstance(kSuppArrayIID);
var length = aArgumentsArray?aArgumentsArray.length:0;
for (var i = 0; i < length; ++i)
{
var rArc = RDF.GetResource(aArgumentsArray[i].property);
argsArray.AppendElement(rArc);
var rValue = null;
if ("resource" in aArgumentsArray[i])
rValue = RDF.GetResource(aArgumentsArray[i].resource);
else
rValue = RDF.GetLiteral(aArgumentsArray[i].literal);
argsArray.AppendElement(rValue);
}
// Exec the command in the Bookmarks datasource.
BMKS.DoCommand(sourcesArray, rCommand, argsArray);
}
/*!
* Delete a resource and all its descendant (hard way)
*/
function deleteResource( ds, aSource )
{
var arcs = ds.ArcLabelsOut(aSource);
while (arcs.hasMoreElements())
{
var property = arcs.getNext().QueryInterface(CI.nsIRDFResource);
var target = ds.GetTarget(aSource,property,true);
// Test if the arc points to a child.
if(RDFC.IsOrdinalProperty(property))
{
// Recusively delete childs
deleteResource(ds,target.QueryInterface(CI.nsIRDFResource));
}
ds.Unassert(aSource,property,target);
}
}
/*!
* Remove childs of a resource but not the resource itself
*/
function removeChilds( ds, aSource )
{
var container = CL["@mozilla.org/rdf/container;1"]
.createInstance(CI.nsIRDFContainer);
container.Init(ds,aSource);
var elems = new Array();
var elements = container.GetElements();
while(elements.hasMoreElements())
elems.push(elements.getNext()
.QueryInterface(CI.nsIRDFResource));
for( var i=0;i<elems.length;++i )
{
var res = elems[i];
if(res == BMKS.getBookmarksToolbarFolder())
removeChilds(ds,res);
else {
container.RemoveElement( res, false );
deleteResource(ds,res);
}
}
}
function spliceContainer( ds , aRes, bRes )
{
var aContainer = CL["@mozilla.org/rdf/container;1"].createInstance(CI.nsIRDFContainer);
var bContainer = CL["@mozilla.org/rdf/container;1"].createInstance(CI.nsIRDFContainer);
aContainer.Init(ds,aRes);
bContainer.Init(ds,bRes);
var elements = aContainer.GetElements();
while(elements.hasMoreElements())
{
var item = elements.getNext().QueryInterface(CI.nsIRDFNode);
bContainer.AppendElement(item);
aContainer.RemoveElement(item,false);
}
}
function backup( file ) {
try {
if (!BMKS) {
BMKS = RDF.GetDataSource("rdf:bookmarks");
BMKS.QueryInterface(CI.nsIBookmarksService);
}
BMKS.QueryInterface(CI.nsIRDFRemoteDataSource);
BMKS.Flush();
// Copy current Bookmark file
var bookmarksFile = DIRS.get("BMarks",CI.nsIFile);
bookmarksFile.copyTo(file.parent,file.leafName);
} catch(e) {
log.exception(e);
}
}
function restore( file ) {
try {
if (!BMKS) {
BMKS = RDF.GetDataSource("rdf:bookmarks");
BMKS.QueryInterface(CI.nsIBookmarksService);
}
BMKS.beginUpdateBatch();
var root = RDF.GetResource("NC:BookmarksRoot");
// Clear bookmarks
removeChilds(BMKS,root);
// Import bookmarks from archives
var args = [{ property: NC_NS+"URL", literal: file.path }];
doBookmarksCommand(root, NC_NS_CMD+"import", args);
var container = CL["@mozilla.org/rdf/container;1"].createInstance(CI.nsIRDFContainer);
container.Init(BMKS,root);
var elements = container.GetElements();
// Toolbar folder is an immutable element
// and the personal toolbar is hooked on it.
// So we need to move content from stored toolbarfolder
// to the actual toolbarfolder.
// All is done under the assumption that the toolbarfolder
// is the first element in the root container
var toolbarFolder = BMKS.getBookmarksToolbarFolder();
var toolbarName = getDefaultPersonalToolbarName();
// Find new toolbar loaded by import command (but not filled into the real personal toolbar :/
var newToolbar=null;
while(elements.hasMoreElements()) {
var res=elements.getNext().QueryInterface(CI.nsIRDFResource);
if (RDFGetName(res)==toolbarName && res!=toolbarFolder) {
newToolbar=res;
break;
}
}
// Fill the correct toolbar
if (newToolbar) {
if(toolbarFolder) {
spliceContainer(BMKS,newToolbar,toolbarFolder);
container.RemoveElement(newToolbar,true);
} else {
// Hum, no toolbar folder set the first entry
// from our new bookmarks set
BMKS.setBookmarksToolbarFolder(newToolbar);
}
}
BMKS.endUpdateBatch();
// Force refresh immediately
BkmsObserverFF2.forceUpdate();
return true;
} catch(e) {
log.exception(e);
}
}